﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace odinshrine
{

    class GestionPhysics
    {
        public int sol_y = 540;
        private Vector2 posCaisse = new Vector2(-1,-1);
        public Vector2 gravity = new Vector2(0, 1), jumpDirection = new Vector2(0, 0);
        private Elts_P tester;
        
        public bool collisionRectangleRectangle(Rectangle objet1, Rectangle objet2)
        {
            return (Rectangle.Intersect(objet1, objet2) != Rectangle.Empty);
        }

        private Vector2 saut(Vector2 playerPosition, GameTime gameTime)
        {
            jumpDirection += gravity * 2.1f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            playerPosition += 10 * jumpDirection;

            return playerPosition;
        }

        public void Saut(Humain player, GraphicsDeviceManager graphics, GameTime gameTime)
        {
            // Gestion saut
            if (player.sauter && collisionRectangleRectangle(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.width, player.height), new Rectangle(0, sol_y - 1, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight - sol_y)))
            {
                // Si le joueur est dans un saut MAIS qu'il touche le sol, ca veut dire qu'il fini son saut, donc reinitialisation des variables relatives a la gravite
                player.sauter = false;
                jumpDirection = new Vector2(0, -1.3f);
            }
            else if (player.sauter && !collisionRectangleRectangle(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.width, player.height), new Rectangle(0, sol_y, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight - sol_y)))
            {
                // Si le joueur est dans le saut et qu'il ne touche pas le sol, on continue de calculer sa position
                player.Position = saut(player.Position, gameTime);

                if (player.Position.Y >= sol_y - player.height)
                {
                    // Petit reglage pour que le joueur ne s'enfonce pas dans le sol
                    player.Position.Y = sol_y - player.height;
                }
            }
        }

        //Collisions du joueur
        public void Maping(Humain player, GraphicsDeviceManager graphics, map Map)
        {
            // Gestion collisions
            if ( player.sauter && player.Position.X < 0)
            {
                // Si le joueur sort de l'ecran, on le repousse dans l'ecran du meme nombre de pixel. A l'affichage, on a l'impression qu'il est bloque contre un mur. Meme principe par la suite.
                player.Position.X += player.Vitesse;
            }
            else if (player.sauter && player.Position.X > (graphics.PreferredBackBufferWidth - player.width))
            {
                player.Position.X -= player.Vitesse;
            }
            else if (Collisions.Ecran(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.width, player.height), graphics))
            {
                player.Position = player.lastPos;
            }


            for (int i = 0; i < Map.caisseList2.GetLength(1); i++)
            {
               for (int j = 0; j < Map.caisseList2.GetLength(0); j++)
            {
                // Joueur / Map
                if (Map.caisseList2[j,i] != null)
                {
                    if (Collisions.Rectangles(new Rectangle((int)player.Position.X,(int) player.Position.Y,(int) player.width,(int) player.height), new Rectangle((j * 34) - Map.coefx, i * 34 - Map.coefy, 34,34)))
                    {
                        if(Collisions.Pixel(Collisions.TextureColor(player),Collisions.TextureColor(Map.caisseList2[j,i].sprte_e),player.Position, new Vector2(j * 34 - Map.coefx, i * 34 - Map.coefy)))
                        {
                         tester = Map.caisseList2[j,i].passability;

                        if ((tester != Elts_P.Bot || tester != Elts_P.All) && (player.Position.Y + player.height) <=  i * 34 + player.Vitesse - Map.coefy)
                        {
                            player.sauter = false;
                            jumpDirection = new Vector2(0, -1.3f);
                            player.Position.Y = i*34 - player.height;

                            posCaisse.X = j * 34;
                            posCaisse.Y = i * 34;
                        }
                        else if ((tester != Elts_P.All || tester != Elts_P.Top) && player.Position.Y <= ( i * 34 + 34 ) - Map.coefy)
                        {
                            jumpDirection.Y = 0;
                            player.Position.Y = player.lastPos.Y;
                        }

                        if (player.Position.X <= j * 34 + 34 - Map.coefx && player.Position.X >= j * 34 - player.Vitesse - Map.coefx && (player.Position.Y <= (i + 1) * 34 - 15 && player.Position.Y + player.height >= (i * 34) + 5))
                        {
                              player.Position.X += player.Vitesse;
                        }
                              else if ((player.Position.X + player.width) >= j * 34 - Map.coefx && (player.Position.X + player.width) <= j * 34 + 34 + player.Vitesse - Map.coefx && (player.Position.Y <= (i + 1) * 34 - 15 && player.Position.Y + player.height >= (i * 34)  + 5))
                        {
                            player.Position.X -= player.Vitesse;
                        }
                    }
                    }
                }
               }
            }
        
            if (posCaisse.X != -1 && posCaisse.Y != -1 && !player.sauter)
            {
                if (posCaisse.X - Map.coefx > (player.Position.X + player.width) || (posCaisse.X - Map.coefx + 34) < player.Position.X)
                {
                    player.sauter = true;
                    jumpDirection = Vector2.Zero;

                    posCaisse.X = -1;
                    posCaisse.Y = -1;
                }
            }
        }

    }
}
